package com.clqteam.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.clqteam.dao.SupplierDao;
import com.clqteam.model.Supplier;
import com.clqteam.util.JDBCutil;
/**查询供应商信息
 * 按照分页显示
 * @author lxg
 *
 */
public class SupplierDaoImpl implements SupplierDao{
	/**
	 * 供应商总数
	 * @return int
	 */
	@Override
	public int countSupplier() {
		int count=0;
		StringBuffer sql=new StringBuffer("");
		sql.append(" SELECT COUNT(s_supplier.supId) AS co");
		sql.append(" FROM s_supplier");
		Connection con=JDBCutil.getConnection();
		try {
			PreparedStatement ps=con.prepareStatement(sql.toString());			
			ResultSet rs=ps.executeQuery();
			if(rs.next()){
				count=rs.getInt("co");
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return count;
	}
	/**
	 * 按页展示供应商信息
	 * @param page 页面
	 * @param rows 页面大小
	 * @return list
	 */
	@Override
	public List<Supplier> selectSupplier(int page, int rows) {
		List<Supplier> li=new ArrayList<Supplier>();
		StringBuffer sql=new StringBuffer("");
		sql.append(" SELECT s_supplier.supId,s_supplier.supName,s_supplier.supContacts,s_supplier.supPhone,s_supplier.eId");
		sql.append(" FROM s_supplier");
		sql.append(" LIMIT ?,?");
		Connection con=JDBCutil.getConnection();
		try {
			PreparedStatement ps=con.prepareStatement(sql.toString());
			ps.setInt(1, (page-1)*rows);
			ps.setInt(2, rows);
			ResultSet rs=ps.executeQuery();
			while (rs.next()){
				Supplier su=new Supplier();
				su.setSupId(rs.getInt("supId"));
				su.setSupName(rs.getString("supName"));
				su.setSupContacts(rs.getString("supContacts"));
				su.setSupPhone(rs.getString("supPhone"));
				su.seteId(rs.getInt("eId"));
				li.add(su);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return li;
	}

}
